<?php
/**
 * User: liwentao
 * Date: 7/20/15
 * Time: 17:34
 */

?>
<script>
	
    function save_prescription(){
    	
    	var medicine_all = new Array();
    	var yongfa = {'42':'口服','2':'静脉注射','1':'外用','8':'冲服','9':'煎服'};
    	var pindu = {'1':'口服','2':'静脉注射','3':'外用','4':'冲服','5':'煎服'};

    	$(".prescription_medicine").each(function(){
    		var medicine_ = {};
			medicine_['day'] = $(this).find('td input[name="day"]').val();
			medicine_['medicine_name'] = $(this).find('td input[name="medicine_name"]').val();
			medicine_['yongliang'] = $(this).find('td input[name="yongliang"]').val();
			
			medicine_['yongfa'] = yongfa[$(this).find('td select[name="yongfa"]').val()];
			medicine_['beizhu'] = $(this).find('td input[name="beizhu"]').val();
			medicine_['pindu'] = pindu[$(this).find('td select[name="pindu"]').val()];
			medicine_['count'] = $(this).find('td input[name="count"]').val();
			medicine_['price'] = $(this).find('td input[name="price"]').val();
			medicine_all.push(medicine_);
			
		});
		medicine_result = JSON.stringify(medicine_all);
		if($('#outpatient_diagnosis').val() == ''){
			alert('诊断内容不能为空');
			return false;
		}
		if($('#outpatient_exhort').val() == ''){
			alert('医生嘱咐不能为空');
			return false;
		}
        $.ajax({
            type: 'POST',
            url: '<?php echo CController::createUrl('prescription/SavePatientPrescription',array('pk_outpatient_id'=>$pk_outpatient_id)) ;?>',
            data:{diagnosis:$('#outpatient_diagnosis').val(),exhort:$('#outpatient_exhort').val(),medicine:medicine_result},
            success: function(data){
                if(data.status==0){
                    alert('保存成功！');
                    var url = '<?php echo CController::createUrl('prescription/FindPerscription',array('pk_outpatient_id'=>$pk_outpatient_id)) ;?>&pk_outpatient_prescription_id='+data.result;
                    addPatientTab('历史处方',url);
                    //$('#main-tab').tabs('getSelected').panel('refresh', url);

                    
                    $('#prescription').treegrid('load',{
                     url: "<?php echo CController::createUrl('prescription/getprescriptions',array('pk_outpatient_id'=>$outpatient->pk_outpatient_id)) ;?>"
                    });
                }else{
                    alert('保存失败！\n请添加完整信息，谢谢！');
                }
            },
            dataType: 'json'
        });
    }

    function count(){
    	var count = 0;
        $(".prescription_medicine").each(function(){
        	if($(this).find('td input[name="price"]').val() == ''){
        		alert('清楚输入价格');return false;
        	}
        	if($(this).find('td input[name="count"]').val() == ''){
        		alert('清楚输入数量');return false;
        	}
            count = count + parseFloat($(this).find('td input[name="price"]').val()) * parseFloat($(this).find('td input[name="count"]').val());
        })
        $('#count_money').html('金额：'+count);
    }

</script>
<div class="easyui-layout" data-options="border:false,fit:true">
    <div data-options="region:'east',selected:true,href:'<?php echo CController::createUrl('medicine/getMedicine'); ?>',iconCls:'icon-pill',title:'药品',split:true" style="width:340px;"></div>

    <div data-options="region:'center',border:false,fit:true" style="padding:5px;width:1100px;">

		<div class="easyui-panel" data-options="border:false" style="padding:5px;width:1000px;">
		    <div class="easyui-panel" data-options="border:false" style="width:100%;height:36px;background:#fafafa;margin-bottom: 10px;padding-top:5px;padding-left: 5px;">
		        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="save_prescription();">保存处方</a>
		    </div>
		    <div class="easyui-panel" data-options="border:false" style="width:100%;height:30px;background:#fafafa;margin-bottom: 5px;">
		        <div class="easyui-layout" data-options="border:false,fit:true">
		            <div data-options="region:'west',border:false" style="width:33%;padding:5px;">患者姓名：&nbsp;&nbsp;<?php echo $outpatient['true_name'];?></div>
		            <div data-options="region:'center',border:false" style="padding:5px;">患者性别：&nbsp;&nbsp;<?php
		                                if($outpatient['gender']==1){
		                                    echo '男';
		                                }else if($outpatient['gender']==2){
		                                    echo '女';
		                                }else {
		                                    echo '保密';
		                                }
		                            ?></div>
		            <div data-options="region:'east',border:false" style="width:33%;padding:5px;">患者年龄：&nbsp;&nbsp;<?php echo age($outpatient['dob']);?></div>
		        </div>
		    </div>
		    <form id="form-prescription" method="post">
		        <input type="hidden" name="fk_outpatient_id" value="<?php echo $pk_outpatient_id; ?>"/>

		        <div class="easyui-accordion" data-options="multiple:true">

		        	<div class="easyui-panel" data-options="border:true,title:'患者信息',selected:false" style="width:100%;padding:5px;background:#fafafa;margin-bottom: 10px;">
		            	<div class="table-patient">
		                <table style="width:100%;">
		                    <tbody>
		                    <tr>
		                        <th align="right">真实姓名：</th>
		                        <td><?php echo $outpatient['true_name'];?></td>
		                        <th align="right">性别：</th>
		                        <td>
		                            <?php
		                                if($outpatient['gender']==1){
		                                    echo '男';
		                                }else if($outpatient['gender']==2){
		                                    echo '女';
		                                }else {
		                                    echo '保密';
		                                }
		                            ?>
		                        </td>
		                        <th align="right">电话：</th>
		                        <?php if($outpatient['phone']!=NULL):?>
		                            <td><?php echo $outpatient['phone'];?></td>
		                        <?php else: ?>
		                            <td><input class="easyui-textbox" value="<?php echo $outpatient['phone'];?>"/></td>
		                        <?php endif; ?>
		                    </tr>
		                    <tr>
		                        <!--<th align="right">生日：</th>
		                        <td><input class="easyui-datebox" value="<?php echo $outpatient['dob'];?>"/></td>-->
		                        <th align="right">年龄：</th>
		                        <?php
		                            $age = age($outpatient['dob']);
		                        ?>
		                        <td><?php echo $age;?></td>
		                        <th align="right">职业：</th>
		                        <td><input class="easyui-textbox" style="" name="occupation" value="<?php echo $outpatient['occupation'];?>"></td>
		                    </tr>
		                    <tr>
		                        <th align="right">住址：</th>
		                        <td colspan="5"><input class="easyui-textbox" style="width:50%" name="location" value="<?php echo $outpatientLocation['address'];?>"></td>
		                    </tr>
		                    <tr>
		                        <th align="right">体重：</th>
		                        <td><input class="easyui-textbox" style="" name="weight" value="<?php echo $outpatient['weight'];?>">&nbsp;kg</td>
		                        <th align="right">身高：</th>
		                        <td><input class="easyui-textbox" style="" name="stature" value="<?php echo $outpatient['stature'];?>">&nbsp;cm</td>
		                        <th align="right">民族：</th>
		                        <td><input class="easyui-textbox" style="" name="blood_type" value="<?php echo $outpatient['occupation'];?>"></td>
		                    </tr>
		                    <tr>
		                        <th align="right">饮酒：</th>
		                        <td>
		                            <select class="" name="alcohol">
		                                <option value="1" <?php if($outpatient['alcohol']==1) echo 'selected';?>>无</option>
		                                <option value="2" <?php if($outpatient['alcohol']==2) echo 'selected';?>>很少</option>
		                                <option value="3" <?php if($outpatient['alcohol']==3) echo 'selected';?>>有时</option>
		                                <option value="4" <?php if($outpatient['alcohol']==4) echo 'selected';?>>经常</option>
		                                <option value="5" <?php if($outpatient['alcohol']==5) echo 'selected';?>>总是</option>
		                            </select>
		                        </td>
		                        <th align="right">吸烟：</th>
		                        <td><input class="easyui-textbox" style="width:50px;" name="smoke" value="<?php if($outpatient['smoke']==NULL) echo '0'; else echo $outpatient['smoke']; ?>"> 支/天</td>
		                        <th align="right">血型：</th>
		                        <td>
		                            <select class="" name="blood_type">
		                                <option value="1" <?php if($outpatient['blood_type']==1) echo 'selected';?>>A</option>
		                                <option value="2" <?php if($outpatient['blood_type']==2) echo 'selected';?>>B</option>
		                                <option value="3" <?php if($outpatient['blood_type']==3) echo 'selected';?>>AB</option>
		                                <option value="4" <?php if($outpatient['blood_type']==4) echo 'selected';?>>O</option>
		                            </select>
		                        </td>
		                    </tr>
		                    <tr>
		                        <th align="right" >过敏史：</th>
		                        <td colspan="5"><input class="easyui-textbox" style="width:50%" name="allergy" value="<?php echo $outpatient['allergy'];?>"></td>
		                    </tr>
		                    </tbody>
		                </table>
		            	</div>
		        	</div>
		        
		            <div title="处方" data-options="iconCls:'icon-prescription',selected:true" style="padding:5px;height:500px;">
		                <table cellpadding="5" style="width:100%;">
		                    <tr>
		                        <td style="width:100px;">诊断:</td>
		                        <td><input class="easyui-textbox" type="text" name="diagnosis" data-options="multiline:true" id="outpatient_diagnosis" style="width:70%;height:60px;"  value=""/><span id="error_outpatient_complaint" style="color:red;"></td>
		                    </tr>
		                    <tr>
		                        <td style="width:100px;">医嘱:</td>
		                        <td><input class="easyui-textbox" type="text" name="exhort" data-options="multiline:true" id="outpatient_exhort" style="width:70%;height:60px;"  value=""/><span id="error_outpatient_complaint" style="color:red;"></td>
		                    </tr>
		                    <tr style="background-color: #F7F7F7" width="100px">
		                        <td><a href="#" id="add_med" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加用药</a></td>
		                        <td width="80%"><table title="" style="width:700px;height:250px"
									data-options="singleSelect:true,collapsible:true,method:'get'">
									<thead id="pre_medicine">
										<tr>
											<th style="width:120px">药品名称</th>
											<th style="width:80px">单次用量</th>
											<th style="width:90px">用法</th>
											<th style="width:120px">备注</th>
											<th style="width:60px">频度</th>
											<th style="width:60px">天数</th>
											<th style="width:60px">总量</th>
											<th style="width:60px">价格</th>
											<th style="width:60px">操作</th>
 									</thead>
									</table>
								</td>

								<td width="10%"><a href="#" onclick="count();" class="easyui-linkbutton">统计金额</a><br><b><span id="count_money"></span></b></td>
		                    </tr>
		                </table>
		            </div>	
    			</div>
		    </form>
		    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="save_prescription();">保存处方</a>
		</div>
		<script>
			$('#add_med').click(function(){
				var html = '<tr class="prescription_medicine">';

                    html += '<td style="width:100px" class="medicine_name"><input style="width:100px" name="medicine_name"></td><td style="width:80px"><input style="width:80px" name="yongliang"></td><td><select name="yongfa" style="width:70px;"><option value="42">口服</option><option value="2">静脉注射</option><option value="1">外用</option><option value="8">冲服</option><option value="9">煎服</option></select></td><td style="width:100px"><input style="width:100px" name="beizhu"/></td><td style="width:60px"><select name="pindu"><option value="1">一天一次</option><option value="2">一天两次</option><option value="3">一天三次</option><option value="4">一天四次</option><option value="5">2小时一次</option><option value="6">4小时一次</option><option value="7">6小时一次</option><option value="8">8小时一次</option><option value="12">睡前一次</option></select></td><td style="width:20px"><input style="width:30px" name="day"></td><td style="width:20px"><input style="width:30px" name="count"></td><td style="width:20px" class="price"><input style="width:30px" name="price"></td><td><a href="#" onclick="remove_medicine(this);" class="easyui-linkbutton" data-options="iconCls:icon-remove">删除</a></td><td><td>';
                    html += '</tr>';
                    $("#pre_medicine").append(html);
			})
		</script>

    </div>
</div>
